package com.example.service.impl;

import com.example.dto.ProfilesAddDTO;
import com.example.entity.HealthProfiles;
import com.example.entity.PageBean;
import com.example.entity.User;
import com.example.mapper.ProfilesMapper;
import com.example.service.ProfilesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ProfilesServiceImpl implements ProfilesService {
    @Autowired
    private ProfilesMapper profilesMapper;

    /**
     * 查询档案
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean select(Integer page, Integer pageSize) {
        Long count = profilesMapper.count();
        Integer start = (page-1)*pageSize;
        List<HealthProfiles> list = profilesMapper.pag(start, pageSize);
        PageBean pageBean = new PageBean(count, list);
        return pageBean;
    }

    @Override
    public void delete(Integer id) {
        profilesMapper.delete(id);
    }

    @Override
    public void update(ProfilesAddDTO profilesAddDTO) {
        HealthProfiles profiles = new HealthProfiles();
        BeanUtils.copyProperties(profilesAddDTO,profiles);
        profiles.setCreateTime(LocalDate.now());
        profiles.setUpdateTime(LocalDate.now());
       // profiles.setBmi(profiles.getWeight()/(profiles.getHeight()*profiles.getHeight()));
        profilesMapper.update(profiles);
    }

    @Override
    public HealthProfiles getId(Integer id) {
        return profilesMapper.getId(id);
    }

    @Override
    public void add(ProfilesAddDTO profilesAddDTO) {
        HealthProfiles profiles = new HealthProfiles();
        BeanUtils.copyProperties(profilesAddDTO,profiles);
        profiles.setCreateTime(LocalDate.now());
        profiles.setUpdateTime(LocalDate.now());
        // profiles.setBmi(profiles.getWeight()/(profiles.getHeight()*profiles.getHeight()));
        profilesMapper.add(profiles);
    }
}
